home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpack-1.2 / amigados.c next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  4.8 KB  |  183 lines

  1. /* (c) Copyright 1993 by Mike W. Meyer
  2.  *
  3.  * Permission to use, copy, modify, distribute, and sell this software
  4.  * and its documentation for any purpose is hereby granted without
  5.  * fee, provided that the above copyright notice appear in all copies
  6.  * and that both that copyright notice and this permission notice
  7.  * appear in supporting documentation, and that the name of Mike W.
  8.  * Meyer not be used in advertising or publicity pertaining to
  9.  * distribution of the software without specific, written prior
  10.  * permission.  Mike W. Meyer makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as
  12.  * is" without express or implied warranty.
  13.  *
  14.  * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  20.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. #include <exec/types.h>
  24. #include <exec/exec.h>
  25. #include <dos/dos.h>
  26.  
  27. #ifdef __SASC
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #else
  31. #include <clib/exec_protos.h>
  32. #include <clib/dos_protos.h>
  33. #endif
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <time.h>
  38. #include <string.h>
  39. #include <errno.h>
  40.  
  41. #include "common.h"
  42. #include "xmalloc.h"
  43.  
  44. #define BADCHARS    "#?()|[]%<>:;$&*\\\ \t\`\""
  45. #define FILELEN        29        /* Max file name length */
  46.  
  47. int overwrite_files = 0 ;
  48. static char *output_fname = NULL;
  49. extern int errno, _OSERR ;
  50. void os_perror(char *) ;
  51. int __buffsize = 8192 ;
  52.  
  53.  
  54. /* Generate a message-id */
  55. char *
  56. os_genid(void) {
  57.     static struct Task *task = NULL ;
  58.     static time_t now ;
  59.     static char hostname[32], domainname[32] ;
  60.     char *result ;
  61.  
  62.     if (task == NULL) {
  63.         task = FindTask(NULL) ;
  64.         time(&now) ;
  65.         if (GetVar("hostname", hostname, sizeof(hostname), 0) < 0)
  66.             strcpy(hostname, "random-amiga") ;
  67.         if (strchr(hostname, '.')) domainname[0] = '\0' ;
  68.         else if (GetVar("domainname", domainname, sizeof(domainname), 0) < 0)
  69.             strcpy(domainname, ".random-domain") ;
  70.         if (domainname[0] && domainname[0] != '.')
  71.             strcpy(&hostname[strlen(hostname)], ".") ;
  72.         }
  73.     result = malloc(25 + strlen(hostname) + strlen(domainname)) ;
  74.     sprintf(result, "%d.%d@%s%s", (long) task, now++, hostname, domainname) ;
  75.     return result ;
  76.     }
  77.     
  78. /* Create and return a directory for a message-id */
  79. char *
  80. os_idtodir(char *id) {
  81.     static char buf[512] ;
  82.     char *p, save ;
  83.     BPTR *dir ;
  84.  
  85.     if (p = getenv("METAMAIL_P_DIR")) strcpy(buf, p) ;
  86.     else strcpy(buf, "t:");
  87.  
  88.     if (!(p = getenv("USERNAME")) && !(p = getenv("USER"))) p = "anonymous" ;
  89.     AddPart(buf, p, sizeof(buf)) ;
  90.     
  91.     if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  92.         os_perror(buf) ;
  93.         return NULL ;
  94.         }
  95.  
  96.     p = buf + strlen(buf) ;
  97.     *p++ = '/' ;
  98.     save = id[FILELEN] ;
  99.     id[FILELEN] = '\0' ;
  100.     while (*id) {
  101.         if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id ;
  102.         id += 1 ;
  103.         }
  104.     *p  = '\0' ;
  105.     id[FILELEN] = save ;
  106.  
  107.     if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  108.         os_perror(buf) ;
  109.         return NULL ;
  110.         }
  111.     strcpy(p, "/") ;
  112.     return buf ;
  113.     }
  114.  
  115. /* Delete the directory created by os_idtodir() */
  116. void
  117. os_donewithdir(char *dir) {
  118.  
  119.     /* Chop off trailing slash */
  120.     dir[strlen(dir) - 1] = '\0' ;
  121.     rmdir(dir) ;
  122.     }
  123.     
  124. /* Create a new file of name "fname". Clean up the name first */
  125. FILE *
  126. os_newtypedfile(char *fname, char *contentType, int binary) {
  127.     char *p, *name, *description, buf[512] ;
  128.     FILE *outfile ;
  129.     static int filesuffix = 0 ;
  130.  
  131.     name = FilePart(fname) ;
  132.  
  133.     /* No BADCHARS */
  134.     for (p = name; *p; p += 1)
  135.         if (!isprint(*p) || strchr(BADCHARS, *p)) *p = 'X' ;
  136.  
  137.     /* Add a count if we don't have a name, or we aren't overwriting */
  138.     if (!*fname || !overwrite_files) {
  139.         if (*name) strcpy(buf, name) ;
  140.         else {
  141.             name = "part" ;
  142.             sprintf(buf, "part.%d", ++filesuffix) ;
  143.             }
  144.         while (outfile = fopen(buf, "r")) {
  145.             if (outfile) fclose(outfile) ;
  146.             sprintf(buf, "%s.%d", name, ++filesuffix) ;
  147.             }
  148.         name = buf ;
  149.         fclose(outfile) ;
  150.         }
  151.  
  152.     if (!(outfile = fopen(name, "w"))) os_perror(name) ;
  153.  
  154.     if (output_fname) free(output_fname) ;
  155.     output_fname = strsave(name) ;
  156.     description = xmalloc(strlen(name) + 6) ;
  157.     strcpy(description, name) ;
  158.     strcat(description, ".desc") ;
  159.     (void) rename(TEMPFILENAME, description) ;
  160.     free(description) ;
  161.  
  162.     fprintf(stdout, "%s (%s)\n", output_fname, contentType) ;
  163.     return outfile ;
  164.     }
  165.  
  166. /* Warn user that MD5 digest didn't match */
  167. void
  168. os_warnMD5mismatch(void) {
  169.     char *warning;
  170.  
  171.     warning = xmalloc(strlen(output_fname) + 100);
  172.     sprintf(warning, "%s was corrupted in transit", output_fname);
  173.     warn(warning);
  174.     free(warning);
  175.     }
  176.  
  177. /* Report an error (in errno) concerning a filename */
  178. void
  179. os_perror(char *file) {
  180.     if (errno != EOSERR) perror(file);
  181.     else poserr(file) ;
  182.     }
  183.